package com.molyfun.parents.view.calendar;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.rest.bean.DateHasDataResponse;
import com.molyfun.parents.utils.LogUtils;

import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.molyfun.parents.view.calendar.DayView.sDayViewHasDataMap;
import static com.molyfun.parents.view.calendar.DayView.sDayViewMineCourseHasDataMap;
import static com.molyfun.parents.view.calendar.DayView.sDayViewTeamCourseHasDataMap;
//import static com.molyfun.parents.view.calendar.DayView.sDayViewHomeworkHasDataMap;

/**
 * Display a month of {@linkplain DayView}s and
 * seven {@linkplain WeekDayView}s.
 */
@SuppressLint("ViewConstructor")
class MonthView extends CalendarPagerView {

    public MonthView(@NonNull MaterialCalendarView view, CalendarDay month, int firstDayOfWeek, int pageIdentify) {

        super(view, month, firstDayOfWeek, pageIdentify);
    }

    public MonthView(@NonNull MaterialCalendarView view, CalendarDay month, int firstDayOfWeek, int pageIdentify, String teamId) {

        super(view, month, firstDayOfWeek, pageIdentify, teamId);
    }

    private static long lastTimeInt = 0l;
    @Override
    protected void buildDayViews(Collection<DayView> dayViews, Calendar calendar) {
        for (int r = 0; r < DEFAULT_MAX_WEEKS; r++) {
            for (int i = 0; i < DEFAULT_DAYS_IN_WEEK; i++) {
                addDayView(dayViews, calendar);
            }
        }
        if(lastTimeInt == 0l){
            getDateDataState(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH)  + "-01");
            lastTimeInt = System.currentTimeMillis();
        }else {
            long nowTime = System.currentTimeMillis();
            if (nowTime - lastTimeInt < 500) {
                try {
                    Thread.sleep(500);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            getDateDataState(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH)  + "-01");
            lastTimeInt = nowTime;
        }

    }

    public CalendarDay getMonth() {
        return getFirstViewDay();
    }

    @Override
    protected boolean isDayEnabled(CalendarDay day) {
        return day.getMonth() == getFirstViewDay().getMonth();
    }

    @Override
    protected int getRows() {
        return DEFAULT_MAX_WEEKS + DAY_NAMES_ROW;
    }

    private void getDateDataState(String date){
        if(currentPage == MaterialCalendarView.PageIdentify.PageMineCourse){
            Call<DateHasDataResponse> call = MFPApplication.getInstance().getRestClient().getTeamDateHasDataList(date, "");
            call.enqueue(new Callback<DateHasDataResponse>() {
                @Override
                public void onResponse(Call<DateHasDataResponse> call, Response<DateHasDataResponse> response) {
                    if(response.body().getCode() == 200){
                        HashMap<String, Boolean> dataMap = new HashMap<>();
                        for(DateHasDataResponse.DateDataBean dateDataBean : response.body().getData()){
                            dataMap.put(dateDataBean.getDatestr(), dateDataBean.isIsexist());
                        }
                        sDayViewMineCourseHasDataMap.putAll(dataMap);
                        updateDateDataState();
                    }

                }

                @Override
                public void onFailure(Call<DateHasDataResponse> call, Throwable t) {
                }
            });
        }else if(currentPage == MaterialCalendarView.PageIdentify.PageCourse){
            LogUtils.d("MainTabCourseFragment"+"monthView");
            Call<DateHasDataResponse> call = MFPApplication.getInstance().getRestClient().getCourseDateHasDataList(date);
            call.enqueue(new Callback<DateHasDataResponse>() {
                @Override
                public void onResponse(Call<DateHasDataResponse> call, Response<DateHasDataResponse> response) {
                    if(response != null && response.body() != null && response.body().getCode() == 200){
                        HashMap<String, Boolean> dataMap = new HashMap<>();
                        for(DateHasDataResponse.DateDataBean dateDataBean : response.body().getData()){
                            dataMap.put(dateDataBean.getDatestr(), dateDataBean.isIsexist());
                        }
                        sDayViewHasDataMap.putAll(dataMap);
                        updateDateDataState();
                    }

                }

                @Override
                public void onFailure(Call<DateHasDataResponse> call, Throwable t) {
                }
            });
        }else if(currentPage == MaterialCalendarView.PageIdentify.PageTeamCourse){
            LogUtils.d("MainTabCourseFragment"+"monthView");
            Call<DateHasDataResponse> call = MFPApplication.getInstance().getRestClient().getTeamDateHasDataList(date, mTeamId);
            call.enqueue(new Callback<DateHasDataResponse>() {
                @Override
                public void onResponse(Call<DateHasDataResponse> call, Response<DateHasDataResponse> response) {
                    if(response != null && response.body() != null && response.body().getCode() == 200){
                        HashMap<String, Boolean> dataMap = new HashMap<>();
                        for(DateHasDataResponse.DateDataBean dateDataBean : response.body().getData()){
                            dataMap.put(dateDataBean.getDatestr(), dateDataBean.isIsexist());
                        }
                        sDayViewTeamCourseHasDataMap.putAll(dataMap);
                        updateDateDataState();
                    }

                }

                @Override
                public void onFailure(Call<DateHasDataResponse> call, Throwable t) {
                }
            });
        }


    }
}
